package common;

import java.io.File;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

public class StringUtil {
	public static final String EMPTY = "";

	public static boolean getString(Object obj) {
		return (obj == null) || ("".equals(obj));
	}

	public static Double getDouble(Object obj) {
		if (obj != null) {
			return Double.valueOf(String.valueOf(obj));
		}
		return Double.valueOf(0.0D);
	}

	public static Integer getInteger(Object obj) {
		if (obj != null) {
			return Integer.valueOf(Integer.parseInt(String.valueOf(obj)));
		}
		return Integer.valueOf(0);
	}

	public static String getDateString(Object obj) {
		if (obj != null) {
			String s = String.valueOf(obj);
			if (s.length() > 10) {
				s = s.substring(0, 10);
			}
			return s;
		}
		return "";
	}

	public static String getDateTimeString(Object obj) {
		if (obj != null) {
			String s = String.valueOf(obj);
			if (s.length() > 19) {
				s = s.substring(0, 19);
			}
			return s;
		}
		return "";
	}

	public static String convertNum2String(int num) {
		return String.valueOf(num + 10000).substring(1);
	}

	public static boolean isNull(Object obj) {
		if ((obj == null) || (obj.equals("")) || (obj.equals("null"))) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(Object obj) {
		return (obj == null) || ("".equals(obj));
	}

	public static int getBoolean(Object obj) {
		if (obj != null) {
			if ((obj instanceof Integer))
				return Integer.parseInt(String.valueOf(obj));
			if (((obj instanceof Boolean))
					&& (obj.equals(Boolean.valueOf(true)))) {
				return 1;
			}
		}
		return 0;
	}

	public static boolean getBoolean(Integer obj) {
		if ((obj == null) || (obj.intValue() == 0)) {
			return false;
		}
		return true;
	}

	public static boolean isNullOrEmpty(String src) {
		return (src == null) || ("".equals(src));
	}

	public static String subLastString(String s) {
		if ((s != null) && (s.length() > 0)) {
			s = s.substring(0, s.length() - 1);
		}
		return s;
	}

	public static String getLikeString(String s) {
		return "%" + s + "%";
	}

	public static String convertNull2String(Object o) {
		String str = String.valueOf(o);
		if ("null".equalsIgnoreCase(String.valueOf(o))) {
			return "";
		}

		return str;
	}

	public static String DateToString(Date date) {
		String strDate = null;
		if(null != date) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				strDate = sdf.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return strDate;
	}

	public static String dateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = null;
		try {
			strDate = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	public static String dateToStringByNumber(Date date, int days) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Integer strDate = Integer.valueOf(0);
		try {
			strDate = Integer.valueOf(Integer.valueOf(
					sdf.format(date).replace("-", "")).intValue()
					- days);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate.toString();
	}

	public static String toMD5(String plainText) {
		String pwdForMD5 = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");

			md.update(plainText.getBytes());

			byte[] b = md.digest();

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				int i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			pwdForMD5 = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pwdForMD5;
	}

	public static String uploadOnlyFile(HttpServletRequest request,
			MultipartFile file) {
		String fName = file.getOriginalFilename();
		String url = null;
		String datePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date())
				.replace("/", "") + "/";
		String path = request.getSession().getServletContext()
				.getRealPath("upload")
				+ "/" + datePath;
		if (file.getSize() > 0L) {
			UUID uuid = UUID.randomUUID();

			String fileName = String.valueOf(new Date().getTime())
					+ uuid.toString().replace("-", "")
					+ fName.substring(fName.lastIndexOf("."));

			File targetFile = new File(path, fileName);
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			try {
				file.transferTo(targetFile);
				url ="/upload/" + datePath + fileName;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return url;
	}

	public static long dats(String dateStart, String dateEnd) {
		long day = 0L;

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date beginDate = format.parse(dateStart);
			Date endDate = format.parse(dateEnd);
			day = (endDate.getTime() - beginDate.getTime()) / 86400000L;
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return day;
	}

	public static Date StringToDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateValue = new Date();
		try {
			dateValue = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateValue;
	}
	
	public static String TimeStamp2Date(String timestampString) {
		Long timestamp = Long.valueOf(Long.parseLong(timestampString));
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
				.format(new Date(timestamp.longValue()));
		return date;
	}
}